<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/index/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>课程管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item label="">
        <el-input size="small" v-model="formInline.userName" placeholder="输入学生名"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table size="small" @selection-change="selectChange" :data="userData" highlight-current-row v-loading="loading"
      element-loading-text="拼命加载中" style="width: 100%">
      <el-table-column type="selection" width="50"> </el-table-column>
      <el-table-column sortable prop="id" label="序号" width="100">
      </el-table-column>
      <el-table-column sortable prop="cou_title" label="课程名" width="100">
      </el-table-column>
      <el-table-column sortable prop="cou_start_time" label="课程开始时间" min-width="60">
      </el-table-column>
      <el-table-column sortable prop="cou_end_time" label="课程结束时间" min-width="60">
      </el-table-column>
      <el-table-column sortable prop="cou_datail" label="授课情况" width="100">
      </el-table-column>
      <el-table-column sortable prop="status" label="签到情况" width="100">
      </el-table-column>
      <el-table-column label="操作" min-width="200">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">详情</el-button>
          <el-button size="mini" type="danger" @click="signIn">签到</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
    <!-- 详情界面 -->
    <el-dialog :title="title" :visible.sync="editFormVisible" width="30%" @click="closeDialog('edit')">
      <el-form label-width="80px" ref="editForm" :model="editForm" :rules="rules">
        <el-form-item label="课程名">
          <el-input v-model="formLabelAlign.cou_title"></el-input>
        </el-form-item>
        <el-form-item label="授课教师">
          <el-input v-model="formLabelAlign.tea_name"></el-input>
        </el-form-item>
        <el-form-item label="开始时间">
          <el-input v-model="formLabelAlign.cou_start_time"></el-input>
        </el-form-item>
        <el-form-item label="结束时间">
          <el-input v-model="formLabelAlign.cou_end_time"></el-input>
        </el-form-item>
        <el-form-item label="简介">
          <el-input v-model="formLabelAlign.con_synopsis"></el-input>
        </el-form-item>
      </el-form>
    </el-dialog>
    <!-- 编辑界面 -->
    <el-dialog :title="title" :visible.sync="codeVisible" width="500px" @click="closeDialog('edit')">
      <div ref="qrcode" style="width:500px; height:300px; margin-top:20px; margin-left:30%;"></div>
        使用微信扫码签到
    </el-dialog>

  </div>
</template>

<script>
  // 导入请求方法
  import {
    userList,
    userSearch
  } from "@/api/user";
 import QRCode from 'qrcodejs2';
  import Pagination from "@/components/Pagination";
  export default {
    data() {
      return {
        loading: false, //是否显示加载
        title: "详情",
         Code_url: 'https://www.baidu.com/',
            qrcode: null,
        codeVisible:false,
        editFormVisible: false,
        formLabelAlign: {
          cou_title: 'java',
          tea_name:"李老师",
          cou_start_time:'2022-06-13 17:18:06',
          cou_end_time:'2022-06-13 19:18:06',
          con_synopsis:"主修"
        },
        
        // 搜索用参数
        formInline: {
          pageNum: 1,
          pageSize: 10,
          userName: "",
        },
        //用户列表数据
        userData: [],
        defaultProps: {
          children: "children",
          label: "name",
        },
        // 选中
        checkmenu: [],
        // 分页参数
        pageparm: {
          currentPage: 1,
          pageSize: 5,
          total: 3,
        },
      };
    },
    // 注册组件
    components: {
      Pagination,
    },
    /**
     * 数据发生改变
     */
    watch: {},

    /**
     * 使用api获得userList
     */
    mounted() {
      this.getdata(this.formInline);
    },

    /**
     * 里面的方法只有被调用才会执行
     */
    methods: {
              
      // 获取数据方法
      getdata(parameter) {
        this.loading = true
        // 模拟数据开始 
        let res = {
          code: 0,
          msg: null,
          count: 12,
          data: [{
              id: '1',
              cou_title: 'java',
              cou_start_time:'2022-06-13 17:18:06',
              cou_end_time:'2022-06-13 19:18:06',
              cou_datail: '已上课',
              status: '已签到',
            },
            {
              id: '2',
              cou_title: 'java',
              cou_start_time:'2022-06-13 17:18:06',
              cou_end_time:'2022-06-13 19:18:06',
              cou_datail: '已上课',
              status: '已签到',
            },
            {
              id: '3',
              cou_title: 'java',
              cou_start_time:'2022-06-13 17:18:06',
              cou_end_time:'2022-06-13 19:18:06',
              cou_datail: '已上课',
              status: '已签到',
            },
            {
              id: '4',
              cou_title: 'java',
              cou_start_time:'2022-06-13 17:18:06',
              cou_end_time:'2022-06-13 19:18:06',
              cou_datail: '已上课',
              status: '已签到',
            },
            {
              id: '5',
              cou_title: 'java',
              cou_start_time:'2022-06-13 17:18:06',
              cou_end_time:'2022-06-13 19:18:06',
              cou_datail: '已上课',
              status: '未签到',
            },
            {
              id: '6',
              cou_title: 'java',
              cou_start_time:'2022-06-13 17:18:06',
              cou_end_time:'2022-06-13 19:18:06',
              cou_datail: '已上课',
              status: '未签到',
            },
            {
              id: '7',
              cou_title: 'java',
              cou_start_time:'2022-06-13 17:18:06',
              cou_end_time:'2022-06-13 19:18:06',
              cou_datail: '已上课',
              status: '已签到',
            },
            {
              id: '8',
              cou_title: 'java',
              cou_start_time:'2022-06-13 17:18:06',
              cou_end_time:'2022-06-13 19:18:06',
              cou_datail: '已上课',
              status: '已签到',
            },
            {
              id: '9',
              cou_title: 'java',
              cou_start_time:'2022-06-13 17:18:06',
              cou_end_time:'2022-06-13 19:18:06',
              cou_datail: '已上课',
              status: '已签到',
            },
            {
              id: '10',
              cou_title: 'java',
              cou_start_time:'2022-06-13 17:18:06',
              cou_end_time:'2022-06-13 19:18:06',
              cou_datail: '已上课',
              status: '未签到',
            }
          ]
        }
        this.loading = false
        this.userData = res.data
        // 分页赋值
        this.pageparm.currentPage = this.formInline.page
        this.pageparm.pageSize = this.formInline.limit
        this.pageparm.total = res.count
      
      },
      // 分页插件事件
      callFather(parm) {
        this.formInline.page = parm.currentPage;
        this.formInline.limit = parm.pageSize;
        this.getdata(this.formInline);
      },
      //搜索事件
      search() {
        this.getdata(this.formInline);
        userSearch(parameter).then((res) => {
      
      if (res.code != 200) {
            this.$message({
              type: "error",
              message: res.message,
            });
          } else {
            this.userData = res.data.list;
            // 分页赋值
            this.pageparm.currentPage = this.formInline.pageNum;
            this.pageparm.pageSize = this.formInline.pageSize;
            this.pageparm.total = res.data.total;
          }
        })
    
      },
      // 选择复选框事件
      selectChange(val) {
        this.selectdata = val;
      },
      //显示编辑界面
      handleEdit: function (index, row) {
        this.editFormVisible = true;
        if (row != undefined && row != "undefined") {
          this.title = "详情";
          this.editForm.cou_title = cou_title;
          this.editForm.cou_start_time = cou_start_time;
          this.editForm.cou_end_time = cou_end_time;
        } else {
          this.title = "详情";
          this.editForm.cou_title = cou_title;
          this.editForm.cou_start_time = cou_start_time;
          this.editForm.cou_end_time = cou_end_time;
        }
      },

    
      // 关闭编辑、增加弹出框
      closeDialog(dialog) {
        if (dialog == "edit") {
          this.editFormVisible = false;
        }
      },
      //签到
      signIn() {
           //生成二维码
     //打开二维码弹窗(设置visible属性)
            this.codeVisible = true

            this.$nextTick(() => {
                console.log("显示二维码的元素(div)=" + this.$refs.qrcode);
                if (this.qrcode != null) {
                    this.qrcode.clear(); // 清除代码
                }
                else {
                    this.qrcode = new QRCode(this.$refs.qrcode, {
                        width: 200,
                        height: 200,
                        correctLevel: QRCode.CorrectLevel.L
                    });

                    // console.log(qrcode);
                    //生成二维码
                    this.qrcode.makeCode(this.Code_url);
                }
            })      
         //TODO
      
    }
  }
  }
</script>

<style scoped>
  .user-search {
    margin-top: 20px;
  }

  .userRole {
    width: 100%;
  }
</style>